﻿using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Model;

namespace Utils.UserHelper;

public class UserChangeInfomation
{
    public static async Task<(bool, string)> ChangeUser(string? userName,
        string? oldPassword, string? newPassword)
    {
        //用户信息修改
        if (userName == null && oldPassword == null)
            return (false, "信息为空");
        await using var db = new DataDbContent();
        if (UserLogin.LoginUser == null)
            return (false, "未登录");
        var userInfo = await db.LoginUser!.FirstOrDefaultAsync(e => e.Account
                                                                   == UserLogin.LoginUser.Account);
        if (userInfo == null)
            return (false, "登录信息错误");
        if (userName != null)
        {
            userInfo.UserName = userName;
        }

        if (oldPassword != null && newPassword != null)
        {
            if (userInfo.Password == oldPassword)
                userInfo.Password = newPassword;
            else
                return (false, "旧密码输入错误");
        }

        await db.SaveChangesAsync();

        return (true, "修改成功");

    }
}